package com.example.gokuplayalong.service.fragments.users.receive;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.core.widget.NestedScrollView;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.gokuplayalong.MainActivity;
import com.example.gokuplayalong.R;
import com.example.gokuplayalong.bases.beans.AppParams;
import com.example.gokuplayalong.bases.beans.Beans;
import com.example.gokuplayalong.bases.custom.CustomGlideEngine;
import com.example.gokuplayalong.bases.custom.textwatcher.CustomLimitInputTextWatcher;
import com.example.gokuplayalong.bases.exception.ExceptionUtils;
import com.example.gokuplayalong.bases.network.NetWorkInterface;
import com.example.gokuplayalong.bases.network.updown.Upload;
import com.example.gokuplayalong.bases.network.updown.UploadDownInterface;
import com.example.gokuplayalong.bases.permission.PermissionUtils;
import com.example.gokuplayalong.bases.permission.PermissionUtilsInterface;
import com.example.gokuplayalong.bases.tools.Logger;
import com.example.gokuplayalong.bases.tools.Tools;
import com.example.gokuplayalong.service.fragmentsmanage.SysManagerFragments;
import com.huawei.hms.ads.App;
import com.kongzue.dialogx.dialogs.BottomDialog;
import com.kongzue.dialogx.dialogs.BottomMenu;
import com.kongzue.dialogx.dialogs.PopMenu;
import com.kongzue.dialogx.dialogs.PopTip;
import com.kongzue.dialogx.interfaces.OnBindView;
import com.kongzue.dialogx.interfaces.OnMenuItemClickListener;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.engine.CompressFileEngine;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;
import com.luck.picture.lib.photoview.PhotoView;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;

import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

import top.zibin.luban.Luban;
import top.zibin.luban.OnNewCompressListener;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link UserMainReceiveUserInfoAddSkills#newInstance} factory method to
 * create an instance of this fragment.
 *
 */
public class UserMainReceiveUserInfoAddSkills extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    View rootView;
    private ImageView mUserMainReceiveUserinfoAddSkillsIv;
    private LinearLayout mUserMainReceiveUserinfoAddSkillsGameType;
    private EditText mUserMainReceiveUserinfoAddSkillsGameTypeInput;
    private LinearLayout mUserMainReceiveUserinfoAddSkillsLayouts;
    private Button mUserMainReceiveUserinfoAddSkillsAddLine;
    private LinearLayout mUserMainReceiveUserinfoAddSkillsImages;
    private ImageView mUserMainReceiveUserinfoAddSkillsUpload;
    private Button mUserMainReceiveUserinfoAddSkillsSubmit;

    private PhotoView mUserMainReceiveUserinfoAddSkillsPhotoview;
    List<LinearLayout> linearLayouts = new ArrayList<>();
    List<EditText> editTextsLabel = new ArrayList<>();
    List<EditText> editTextsDesc = new ArrayList<>();
    List<TextView> textViews = new ArrayList<>();


    List<ImageView> imageViews = new ArrayList<>();
    List<String> imageViewsName = new ArrayList<>();
    LinearLayout linearLayout = new LinearLayout(MainActivity.getInstance());
    JSONObject data = new JSONObject();
    List<Beans> beans = new ArrayList<>();
    OnBindView<BottomDialog> bottomDialogOnBindViewForSorts;

    BottomDialog bottomDialogForSorts;


    public UserMainReceiveUserInfoAddSkills() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment UserMainReceiveUserInfoAddSkills.
     */
    // TODO: Rename and change types and number of parameters
    public static UserMainReceiveUserInfoAddSkills newInstance(String param1, String param2) {
        UserMainReceiveUserInfoAddSkills fragment = new UserMainReceiveUserInfoAddSkills();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (rootView == null)
            rootView = inflater.inflate(R.layout.fragment_user_main_receive_user_info_add_skills, container, false);
        initView();
        return rootView;
    }

    private void initView() {


        mUserMainReceiveUserinfoAddSkillsIv = rootView.findViewById(R.id.user_main_receive_userinfo_add_skills_iv);
        mUserMainReceiveUserinfoAddSkillsPhotoview = rootView.findViewById(R.id.user_main_receive_userinfo_add_skills_photoview);
        mUserMainReceiveUserinfoAddSkillsGameType = rootView.findViewById(R.id.user_main_receive_userinfo_add_skills_game_type);
        mUserMainReceiveUserinfoAddSkillsGameTypeInput = rootView.findViewById(R.id.user_main_receive_userinfo_add_skills_game_type_input);
        mUserMainReceiveUserinfoAddSkillsLayouts = rootView.findViewById(R.id.user_main_receive_userinfo_add_skills_layouts);
        mUserMainReceiveUserinfoAddSkillsAddLine = rootView.findViewById(R.id.user_main_receive_userinfo_add_skills_add_line);
        mUserMainReceiveUserinfoAddSkillsImages = rootView.findViewById(R.id.user_main_receive_userinfo_add_skills_images);
        mUserMainReceiveUserinfoAddSkillsUpload = rootView.findViewById(R.id.user_main_receive_userinfo_add_skills_upload);
        mUserMainReceiveUserinfoAddSkillsSubmit = rootView.findViewById(R.id.user_main_receive_userinfo_add_skills_submit);
        bottomDialogOnBindViewForSorts = new OnBindView<BottomDialog>(R.layout.fragment_user_main_games_sorts) {
            @Override
            public void onBind(BottomDialog dialog, View v) {
                try {
                    View.OnClickListener onClickListener = new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {


                            String selectItem = "王者荣耀";
                            try {

                                if (v.getId() == R.id.user_main_game_sorts_menu6_page_gk) {
                                    bottomDialogForSorts.dismiss();
                                    selectItem = "王者荣耀";
                                }
                                else if (v.getId() == R.id.user_main_game_sorts_menu6_page_hero) {
                                    bottomDialogForSorts.dismiss();
                                    selectItem = "英雄联盟";
                                }
                                else if (v.getId() == R.id.user_main_game_sorts_menu6_page_peace) {
                                    bottomDialogForSorts.dismiss();
                                    selectItem = "和平精英";
                                }

                            } catch (Throwable e) {
                                ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                            }


                            try {

                                mUserMainReceiveUserinfoAddSkillsLayouts.removeAllViews();
                                linearLayouts.clear();
                                editTextsLabel.clear();
                                editTextsDesc.clear();
                                textViews.clear();


                                try {
                                    int count = 0;
                                    for (int i = 0; i < beans.size(); i++) {
                                        if (beans.get(i)
                                                .getJsonObject()
                                                .getString("gametype")
                                                .equals(selectItem)) {
                                            JSONObject data = JSON.parseObject(beans.get(i)
                                                    .getJsonObject()
                                                    .getString("data"));

                                            Iterator<String> sIterator = data.keySet()
                                                    .iterator();
                                            while (sIterator.hasNext()) {
                                                String key = sIterator.next();
                                                if (!Objects.equals(key, "imageViewsName")) {
                                                    addLine(key, data.getString(key));
                                                    count++;
                                                }
                                            }

                                            break;
                                        }
                                    }

                                    if (count == 0) {

                                        addLine("擅长位置", "中路、打野、对抗路、发育路");
                                        addLine("擅长角色", "云缨 艾琳 司空震 澜 夏洛特 阿古朵 蒙恬");
                                        addLine("游戏大区", "微信区");

                                    }

                                } catch (Throwable e) {

                                }
                                mUserMainReceiveUserinfoAddSkillsGameTypeInput.setText(selectItem);


                                imageViews.clear();
                                imageViewsName.clear();
                                mUserMainReceiveUserinfoAddSkillsImages.removeAllViews();
                            } catch (Throwable e) {
                                ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                            }
                        }
                    };
                    Button button = v.findViewById(R.id.user_main_game_sorts_menu6_page_gk);
                    button.setOnClickListener(onClickListener);
                    button = v.findViewById(R.id.user_main_game_sorts_menu6_page_hero);
                    button.setOnClickListener(onClickListener);
                    button = v.findViewById(R.id.user_main_game_sorts_menu6_page_peace);
                    button.setOnClickListener(onClickListener);


                } catch (Throwable e) {
                    ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                }


                try {


                    TextView mUserMainGameSortsMenu1;
                    TextView mUserMainGameSortsMenu2;
                    TextView mUserMainGameSortsMenu3;
                    TextView mUserMainGameSortsMenu4;
                    TextView mUserMainGameSortsMenu5;
                    TextView mUserMainGameSortsMenu6;
                    NestedScrollView mUserMainGameSortsMenu1Page;
                    NestedScrollView mUserMainGameSortsMenu2Page;
                    NestedScrollView mUserMainGameSortsMenu3Page;
                    NestedScrollView mUserMainGameSortsMenu4Page;
                    NestedScrollView mUserMainGameSortsMenu5Page;
                    NestedScrollView mUserMainGameSortsMenu6Page;


                    mUserMainGameSortsMenu1 = v.findViewById(R.id.user_main_game_sorts_menu1);
                    mUserMainGameSortsMenu2 = v.findViewById(R.id.user_main_game_sorts_menu2);
                    mUserMainGameSortsMenu3 = v.findViewById(R.id.user_main_game_sorts_menu3);
                    mUserMainGameSortsMenu4 = v.findViewById(R.id.user_main_game_sorts_menu4);
                    mUserMainGameSortsMenu5 = v.findViewById(R.id.user_main_game_sorts_menu5);
                    mUserMainGameSortsMenu6 = v.findViewById(R.id.user_main_game_sorts_menu6);
                    mUserMainGameSortsMenu1Page = v.findViewById(R.id.user_main_game_sorts_menu1_page);
                    mUserMainGameSortsMenu2Page = v.findViewById(R.id.user_main_game_sorts_menu2_page);
                    mUserMainGameSortsMenu3Page = v.findViewById(R.id.user_main_game_sorts_menu3_page);
                    mUserMainGameSortsMenu4Page = v.findViewById(R.id.user_main_game_sorts_menu4_page);
                    mUserMainGameSortsMenu5Page = v.findViewById(R.id.user_main_game_sorts_menu5_page);
                    mUserMainGameSortsMenu6Page = v.findViewById(R.id.user_main_game_sorts_menu6_page);


                    mUserMainGameSortsMenu1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mUserMainGameSortsMenu1.setTextColor(Color.BLUE);
                            mUserMainGameSortsMenu2.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu3.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu4.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu5.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu6.setTextColor(Color.BLACK);


                            mUserMainGameSortsMenu1Page.setVisibility(View.VISIBLE);
                            mUserMainGameSortsMenu2Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu3Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu4Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu5Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu6Page.setVisibility(View.GONE);

                        }
                    });
                    mUserMainGameSortsMenu2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mUserMainGameSortsMenu1.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu2.setTextColor(Color.BLUE);
                            mUserMainGameSortsMenu3.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu4.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu5.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu6.setTextColor(Color.BLACK);

                            mUserMainGameSortsMenu1Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu2Page.setVisibility(View.VISIBLE);
                            mUserMainGameSortsMenu3Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu4Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu5Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu6Page.setVisibility(View.GONE);

                        }
                    });
                    mUserMainGameSortsMenu3.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mUserMainGameSortsMenu1.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu2.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu3.setTextColor(Color.BLUE);
                            mUserMainGameSortsMenu4.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu5.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu6.setTextColor(Color.BLACK);

                            mUserMainGameSortsMenu1Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu2Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu3Page.setVisibility(View.VISIBLE);
                            mUserMainGameSortsMenu4Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu5Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu6Page.setVisibility(View.GONE);

                        }
                    });
                    mUserMainGameSortsMenu4.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mUserMainGameSortsMenu1.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu2.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu3.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu4.setTextColor(Color.BLUE);
                            mUserMainGameSortsMenu5.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu6.setTextColor(Color.BLACK);

                            mUserMainGameSortsMenu1Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu2Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu3Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu4Page.setVisibility(View.VISIBLE);
                            mUserMainGameSortsMenu5Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu6Page.setVisibility(View.GONE);

                        }
                    });
                    mUserMainGameSortsMenu5.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mUserMainGameSortsMenu1.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu2.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu3.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu4.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu5.setTextColor(Color.BLUE);
                            mUserMainGameSortsMenu6.setTextColor(Color.BLACK);

                            mUserMainGameSortsMenu1Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu2Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu3Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu4Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu5Page.setVisibility(View.VISIBLE);
                            mUserMainGameSortsMenu6Page.setVisibility(View.GONE);

                        }
                    });
                    mUserMainGameSortsMenu6.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mUserMainGameSortsMenu1.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu2.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu3.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu4.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu5.setTextColor(Color.BLACK);
                            mUserMainGameSortsMenu6.setTextColor(Color.BLUE);

                            mUserMainGameSortsMenu1Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu2Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu3Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu4Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu5Page.setVisibility(View.GONE);
                            mUserMainGameSortsMenu6Page.setVisibility(View.VISIBLE);

                        }
                    });
                } catch (Throwable e) {
                    ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                }
            }
        };

        mUserMainReceiveUserinfoAddSkillsGameTypeInput.addTextChangedListener(new CustomLimitInputTextWatcher(mUserMainReceiveUserinfoAddSkillsGameTypeInput));
        mUserMainReceiveUserinfoAddSkillsUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if (!Tools.Deshak()) return;
                    if (mUserMainReceiveUserinfoAddSkillsGameTypeInput.getText()
                            .toString()
                            .isEmpty()) {
                        PopTip.show("游戏类型未填写");
                        mUserMainReceiveUserinfoAddSkillsGameType.performClick();
                        return;
                    }

                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
                        PermissionUtils.getInstance()
                                .checkPermission(PermissionUtils.permission_storage_sdk33, "悟空陪玩正在申请存储权限，用于上传截图、证明等", PermissionUtils.requestCode_storage_sdk33, new PermissionUtilsInterface() {
                                    @Override
                                    public void hasPermissions() {
                                        imageUpload();
                                    }

                                    @Override
                                    public void onPermissionsGranted(int i, @NonNull List<String> list) {
                                        imageUpload();
                                    }

                                    @Override
                                    public void onPermissionsDenied(int i, @NonNull List<String> list) {
                                        PopTip.show("权限被拒绝");
                                    }
                                });
                    }
                    else {
                        PermissionUtils.getInstance()
                                .checkPermission(PermissionUtils.permission_storage, "悟空陪玩正在申请存储权限，用于上传截图、证明等", PermissionUtils.requestCode_storage, new PermissionUtilsInterface() {
                                    @Override
                                    public void hasPermissions() {
                                        imageUpload();
                                    }

                                    @Override
                                    public void onPermissionsGranted(int i, @NonNull List<String> list) {
                                        imageUpload();
                                    }

                                    @Override
                                    public void onPermissionsDenied(int i, @NonNull List<String> list) {
                                        PopTip.show("权限被拒绝");
                                    }
                                });
                    }
                } catch (Throwable e) {
                    ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                }
            }
        });

        mUserMainReceiveUserinfoAddSkillsGameType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if (!Tools.Deshak()) return;

                    bottomDialogForSorts = BottomDialog.show("游戏分类", "")
                            .setCustomView(bottomDialogOnBindViewForSorts);


                } catch (Throwable e) {
                    ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                }
            }
        });

        mUserMainReceiveUserinfoAddSkillsAddLine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if (!Tools.Deshak()) return;
                    LinearLayout childView = (LinearLayout) LayoutInflater.from(requireContext())
                            .inflate(R.layout.fragment_user_main_receive_user_info_add_skills_template, mUserMainReceiveUserinfoAddSkillsLayouts, false);
                    mUserMainReceiveUserinfoAddSkillsLayouts.addView(childView);

                    linearLayouts.add(childView);
                    EditText editText = childView.findViewById(R.id.user_main_receive_userinfo_add_skills_template_label);
                    editText.setId(("edittext_" + linearLayouts.size() + "_label").hashCode());
                    editText.addTextChangedListener(new CustomLimitInputTextWatcher(editText));
                    editTextsLabel.add(editText);

                    editText = childView.findViewById(R.id.user_main_receive_userinfo_add_skills_template_desc);
                    editText.setId(("edittext_" + linearLayouts.size() + "_desc").hashCode());
                    editText.addTextChangedListener(new CustomLimitInputTextWatcher(editText));
                    editTextsDesc.add(editText);

                    TextView textView = childView.findViewById(R.id.user_main_receive_userinfo_add_skills_template_delete);
                    textView.setId(("textview_" + linearLayouts.size() + "_delete").hashCode());
                    textView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            try {
                                for (int i = 0; i < textViews.size(); i++) {
                                    if (view.getId() == textViews.get(i).getId()) {
                                        mUserMainReceiveUserinfoAddSkillsLayouts.removeView(linearLayouts.get(i));
                                        linearLayouts.remove(i);
                                        editTextsLabel.remove(i);
                                        editTextsDesc.remove(i);
                                        textViews.remove(i);
                                        break;
                                    }
                                }
                            } catch (Throwable e) {
                                ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                            }
                        }
                    });
                    textViews.add(textView);


                } catch (Throwable e) {
                    ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                }
            }
        });
        mUserMainReceiveUserinfoAddSkillsIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if (!Tools.Deshak()) return;
                    SysManagerFragments.switchBackOnSysFragment();
                } catch (Throwable e) {
                    ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                }
            }
        });
        mUserMainReceiveUserinfoAddSkillsSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if (!Tools.Deshak()) return;
                    if (data != null && !data.isEmpty()) data.clear();
                    if (mUserMainReceiveUserinfoAddSkillsGameTypeInput.getText()
                            .toString()
                            .isEmpty()) {
                        PopTip.show("游戏类型不能为空!");
                        return;
                    }

                    for (int i = 0; i < linearLayouts.size(); i++) {
                        if (editTextsLabel.get(i).getText().toString().isEmpty()) {
                            PopTip.show("未填写完成!");
                            return;
                        }
                        if (editTextsDesc.get(i).getText().toString().isEmpty()) {
                            PopTip.show("未填写完成!");
                            return;
                        }
                    }

                    for (int i = 0; i < linearLayouts.size(); i++) {
                        data.put(editTextsLabel.get(i).getText().toString(), editTextsDesc.get(i)
                                .getText()
                                .toString());
                    }

                    data.put("imageViewsName", String.join(",", imageViewsName));

                    Beans beansIn = new Beans();
                    beansIn.getJsonObject()
                            .put("phone", AppParams.getInstance().appCurrentUser.getJsonObject()
                                    .getString("phone"));
                    beansIn.getJsonObject()
                            .put("gametype", mUserMainReceiveUserinfoAddSkillsGameTypeInput.getText()
                                    .toString());

                    beansIn.getJsonObject().put("data", data.toJSONString());


                    AppParams.getNewNetWork()
                            .setInterface(new NetWorkInterface() {
                                @Override
                                public int success(String msg) {
                                    requireActivity().runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            try {
                                                JSONObject msgFromServer = JSON.parseObject(msg);
                                                if (Objects.equals(msgFromServer.getString("result"), "成功")) {
                                                    PopTip.show("修改成功!");
                                                    SysManagerFragments.switchBackOnSysFragment();
                                                }
                                            } catch (Throwable e) {
                                                ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                                            }
                                        }
                                    });
                                    return 0;
                                }

                                @Override
                                public int failed(String msg) {
                                    return 0;
                                }
                            })
                            .execute(AppParams.getInstance().baseUrl + "/modifyMerchantSkills", beansIn.getJsonObject()
                                    .toJSONString());

                } catch (Throwable e) {
                    ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                }
            }
        });
    }


    private void addLine(String label, String desc) {
        try {

            LinearLayout childView = (LinearLayout) LayoutInflater.from(requireContext())
                    .inflate(R.layout.fragment_user_main_receive_user_info_add_skills_template, mUserMainReceiveUserinfoAddSkillsLayouts, false);
            mUserMainReceiveUserinfoAddSkillsLayouts.addView(childView);

            linearLayouts.add(childView);
            EditText editText = childView.findViewById(R.id.user_main_receive_userinfo_add_skills_template_label);
            editText.setId(("edittext_" + linearLayouts.size() + "_label").hashCode());
            editText.setText(label);
            editText.addTextChangedListener(new CustomLimitInputTextWatcher(editText));
            editTextsLabel.add(editText);

            editText = childView.findViewById(R.id.user_main_receive_userinfo_add_skills_template_desc);
            editText.setId(("edittext_" + linearLayouts.size() + "_desc").hashCode());
            editText.setText(desc);
            editText.addTextChangedListener(new CustomLimitInputTextWatcher(editText));
            editTextsDesc.add(editText);

            TextView textView = childView.findViewById(R.id.user_main_receive_userinfo_add_skills_template_delete);
            textView.setId(("textview_" + linearLayouts.size() + "_delete").hashCode());
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    try {
                        for (int i = 0; i < textViews.size(); i++) {

                            if (view.getId() == textViews.get(i)
                                    .getId()) {
                                mUserMainReceiveUserinfoAddSkillsLayouts.removeView(linearLayouts.get(i));
                                linearLayouts.remove(i);
                                editTextsLabel.remove(i);
                                editTextsDesc.remove(i);
                                textViews.remove(i);

                                break;
                            }
                        }
                    } catch (Throwable e) {

                        ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                    }
                }
            });
            textViews.add(textView);


        } catch (Throwable e) {
            ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            initChanged();
        }
        else {
            if (bottomDialogForSorts != null) bottomDialogForSorts.dismiss();
        }
    }

    private void initChanged() {
        try {

            getSkillsList();
        } catch (Throwable e) {
            ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
        }
    }

    private void getSkillsList() {
        try {
            Beans beansIn = new Beans();
            beansIn.getJsonObject()
                    .put("phone", AppParams.getInstance().appCurrentUser.getJsonObject()
                            .getString("phone"));
            AppParams.getNewNetWork()
                    .setInterface(new NetWorkInterface() {
                        @Override
                        public int success(String msg) {
                            try {
                                JSONObject msgFromServer = JSON.parseObject(msg);
                                if (Objects.equals(msgFromServer.getString("result"), "成功")) {
                                    JSONArray skillsList = JSON.parseArray(msgFromServer.getString("skillsList"));
                                    if (!beans.isEmpty()) beans.clear();
                                    for (int i = 0; i < skillsList.size(); i++) {
                                        Beans item = new Beans();
                                        item.setJsonObject(skillsList.getJSONObject(i));
                                        beans.add(item);
                                    }
                                }
                            } catch (Throwable e) {
                                ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                            }


                            return 0;
                        }

                        @Override
                        public int failed(String msg) {
                            return 0;
                        }
                    })
                    .execute(AppParams.getInstance().baseUrl + "/getSkillsList", beansIn.getJsonObject()
                            .toJSONString());
        } catch (Throwable e) {
            ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
        }
    }

    private void imageUpload() {
        try {
            PictureSelector.create(MainActivity.getInstance())
                    .openGallery(SelectMimeType.ofImage())
                    .setCompressEngine((CompressFileEngine) ((context, source, call) -> {
                        Luban.with(context)
                                .load(source)
                                .setCompressListener(new OnNewCompressListener() {
                                    @Override
                                    public void onStart() {
                                        Logger.d("onStart");
                                    }

                                    @Override
                                    public void onSuccess(String source, File compressFile) {
                                        Logger.d("onSuccess");
                                        if (call != null) {
                                            call.onCallback(source, compressFile.getAbsolutePath());
                                        }
                                    }

                                    @Override
                                    public void onError(String source, Throwable e) {
                                        Logger.d("onError " + e.getMessage());
                                        if (call != null) {
                                            call.onCallback(source, null);
                                        }
                                    }
                                })
                                .launch();
                    }))
                    .isDisplayCamera(false)
                    .setMaxSelectNum(1)
                    .setImageEngine(CustomGlideEngine.createGlideEngine())
                    .forResult(new OnResultCallbackListener<LocalMedia>() {
                        @Override
                        public void onResult(ArrayList<LocalMedia> result) {
                            LocalMedia localMedia = result.get(0);
                            String filename = AppParams.getInstance().appCurrentUser.getJsonObject()
                                    .getString("phone") + "_" + mUserMainReceiveUserinfoAddSkillsGameTypeInput.getText()
                                    .toString() + "_add_kills_" + imageViews.size() + ".png";
                            Upload.getInstance()
                                    .uploadImage(new File(localMedia.getCompressPath()), localMedia.getMimeType(), filename, new UploadDownInterface() {
                                        @Override
                                        public void uploadSuccess(String msg) {
                                            requireActivity().runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    try {
                                                        PopTip.show("上传成功!");
                                                        ImageView imageView = new ImageView(rootView.getContext());
                                                        ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(256, 256);
                                                        imageView.setLayoutParams(layoutParams);
                                                        imageView.setId(("UserMainReceiveUserInfoAddSkills_upload_rand_" + System.currentTimeMillis() + imageViews.size()).hashCode());
                                                        imageView.setOnLongClickListener(new View.OnLongClickListener() {
                                                            @Override
                                                            public boolean onLongClick(View v) {
                                                                try {
                                                                    PopMenu.show(new String[]{"放大", "删除"})
                                                                            .setOnMenuItemClickListener(new OnMenuItemClickListener<PopMenu>() {
                                                                                @Override
                                                                                public boolean onClick(PopMenu dialog, CharSequence text, int index) {
                                                                                    if (text == "放大") {
                                                                                        for (int i = 0; i < imageViews.size(); i++) {
                                                                                            if (v.getId() == imageViews.get(i)
                                                                                                    .getId()) {
                                                                                                Picasso.with(MainActivity.getInstance())
                                                                                                        .load(AppParams.getInstance().uploadMediaLocationServer + "/" + imageViewsName.get(i))
                                                                                                        .placeholder(R.drawable.ic_logo)
                                                                                                        .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
                                                                                                        .networkPolicy(NetworkPolicy.NO_CACHE)
                                                                                                        .into(mUserMainReceiveUserinfoAddSkillsPhotoview);
                                                                                                mUserMainReceiveUserinfoAddSkillsPhotoview.setVisibility(View.VISIBLE);
                                                                                                mUserMainReceiveUserinfoAddSkillsPhotoview.setOnClickListener(new View.OnClickListener() {
                                                                                                    @Override
                                                                                                    public void onClick(View v) {
                                                                                                        mUserMainReceiveUserinfoAddSkillsPhotoview.setVisibility(View.GONE);
                                                                                                    }
                                                                                                });
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                    else if (text == "删除") {
                                                                                        for (int i = 0; i < imageViews.size(); i++) {
                                                                                            if (v.getId() == imageViews.get(i)
                                                                                                    .getId()) {
                                                                                                try {
                                                                                                    if (i < imageViews.size())
                                                                                                        imageViews.remove(i);
                                                                                                    if (i < imageViewsName.size())
                                                                                                        imageViewsName.remove(i);
                                                                                                    for (int j = 0; j < mUserMainReceiveUserinfoAddSkillsImages.getChildCount(); j++) {
                                                                                                        linearLayout = (LinearLayout) mUserMainReceiveUserinfoAddSkillsImages.getChildAt(j);
                                                                                                        for (int ii = 0; ii < linearLayout.getChildCount(); ii++) {
                                                                                                            if (v.getId() == linearLayout.getChildAt(ii)
                                                                                                                    .getId()) {
                                                                                                                linearLayout.removeView(v);
                                                                                                            }
                                                                                                        }
                                                                                                    }
//                                                                                                    JSONObject data = JSON.parseObject(beans.getJsonObject()
//                                                                                                            .getString("data"));
//                                                                                                    data.put("imageViewsName", String.join(",", imageViewsName));
//                                                                                                    beans.getJsonObject()
//                                                                                                            .put("data", data.toJSONString());
                                                                                                } catch (
                                                                                                        Throwable e) {
                                                                                                    ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                    return false;
                                                                                }
                                                                            });
                                                                } catch (Throwable e) {
                                                                    ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                                                                }
                                                                return false;
                                                            }
                                                        });
                                                        if (imageViews.size() % 4 == 0) {
                                                            linearLayout = new LinearLayout(rootView.getContext());
                                                            mUserMainReceiveUserinfoAddSkillsImages.addView(linearLayout);
                                                        }
                                                        imageViews.add(imageView);
                                                        imageViewsName.add(filename);
                                                        linearLayout.addView(imageView);
//                                                        JSONObject data = JSON.parseObject(beans.getJsonObject()
//                                                                .getString("data"));
//                                                        data.put("imageViewsName", String.join(",", imageViewsName));
//                                                        beans.getJsonObject()
//                                                                .put("data", data.toJSONString());
                                                        Picasso.with(MainActivity.getInstance())
                                                                .load(AppParams.getInstance().uploadMediaLocationServer + "/" + filename)
                                                                .placeholder(R.drawable.ic_logo)
                                                                .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
                                                                .networkPolicy(NetworkPolicy.NO_CACHE)
                                                                .into(imageView);
                                                    } catch (Throwable e) {
                                                        ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                                                    }
                                                }
                                            });
                                        }

                                        @Override
                                        public void uploadFailed(IOException e) {
                                        }

                                        @Override
                                        public void downSuccess() {
                                        }

                                        @Override
                                        public void downFailed() {
                                        }
                                    });
                        }

                        @Override
                        public void onCancel() {
                        }
                    });
        } catch (Throwable e) {
            ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
        }
    }
}